package com.example.start.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.start.common.MD5;
import com.example.start.entity.SysUser;
import com.example.start.mapper.SysUserMapper;
import com.example.start.mapper.UserMapper;
import com.example.start.req.SysUserLoginReq;
import com.example.start.req.SysUserReq;
import com.example.start.service.SysUserService;
import com.example.start.utils.CopyUtil;
import com.example.start.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

@Service
public class SysUserServiceImpl  extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public void register(SysUserReq sysUserReq) {
        SysUser user = CopyUtil.copy(sysUserReq,SysUser.class);
        if(ObjectUtils.isEmpty(sysUserReq.getId())){
            SysUser sysUser = selectByLongName1(sysUserReq.getLoginName());
            if(ObjectUtils.isEmpty(sysUser)){
                user.setId(snowFlake.nextId());
                sysUserMapper.insert(user);
            }
        }
    }

    @Override
    public SysUserLoginReq login(SysUserLoginReq sysUserLoginReq) {
        SysUser sysUser = selectByLongName(sysUserLoginReq.getLoginName());
        System.out.println(sysUserLoginReq.getPassword());
        System.out.println(MD5.encrypt(sysUserLoginReq.getPassword()));
        SysUser sysUser1 = selectByLongPassword(sysUserLoginReq.getPassword());
        if(ObjectUtils.isEmpty(sysUser)&&ObjectUtils.isEmpty(sysUser1)){
            SysUserLoginReq user = CopyUtil.copy(sysUser, SysUserLoginReq.class);
            return user;
        }else{
            throw new RuntimeException("用户名或密码有误");
        }
    }


    public SysUser selectByLongName(String loginName){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUser::getLoginName,loginName);
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(sysUsers)){
            return  null;//存在
        }else{
            throw new RuntimeException("用户名不存在");
        }
    }
    public SysUser selectByLongName1(String loginName){
        QueryWrapper<SysUser> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().eq(SysUser::getLoginName,loginName);
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper1);
        if(CollectionUtils.isEmpty(sysUsers)){
            return  null;//不存在
        }else{
            throw new RuntimeException("用户名已存在");
        }
    }

    public SysUser selectByLongPassword(String longPassword){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
       wrapper.lambda().eq(SysUser::getPassword,longPassword);
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(sysUsers)){
            return null;//密码正确
        }else{
            throw new RuntimeException("密码错误");
        }
    }
}
